﻿using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Linq;
using Serilog;
using System.IO;
using Wpf.Ui;
using Wpf.Ui.Appearance;
using Wpf.Ui.Controls;
using WpfMvvmTemplate.WPFUI.Common;

namespace WpfMvvmTemplate.WPFUI.Views.Main
{
    public partial class MainWindowView
    {
        public MainWindowView()
        {
            SystemThemeWatcher.Watch(this);

            //读取文件内容
            string fileContent = File.ReadAllText(GlobalStaticData.AppSettingsFilePath);

            //解析文件内容
            JObject? jObject = JObject.Parse(fileContent);
            GlobalStaticData.LanguageNum = (int)(jObject?["AppSettings"]?["Language"]!);

            ComMethod.LanguageWinDicHelper(GlobalStaticData.LanguageNum, this);

            InitializeComponent();
        }

        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FluentWindow_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            //读取文件内容
            string fileContent = File.ReadAllText(GlobalStaticData.AppSettingsFilePath);

            //解析文件内容
            JObject? jObject = JObject.Parse(fileContent);

            switch(jObject?["AppSettings"]?["Theme"]!.ToString())
            {
                case "0": ApplicationThemeManager.Apply(ApplicationTheme.Light); break;
                case "1": ApplicationThemeManager.Apply(ApplicationTheme.Dark); break;
            }

            //容器
            App.Current.Services.GetRequiredService<INavigationService>().SetNavigationControl(NavigationView);
            App.Current.Services.GetRequiredService<ISnackbarService>().SetSnackbarPresenter(SnackbarPresenter);
            App.Current.Services.GetRequiredService<IContentDialogService>().SetContentPresenter(RootContentDialog);

            NavigationView.SetServiceProvider(App.Current.Services.GetRequiredService<IServiceProvider>());

            //测试日志
            //GlobalStaticData.Logger.Information("MainWindowViewModel");
            //GlobalStaticData.Logger.Error("MainWindowViewModel");

        }
    }
}
